package com.zzyl.controller.manager;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/alert-rule")
@Api(tags = "报警规则相关接口")
public class AlertRuleController {
    @Autowired
    private AlertRuleService alertRuleService;

    @ApiOperation("分页获取告警规则列表")
    @GetMapping(path = "/get-page")
    public ResponseResult<PageResponse<AlertRuleVo>> getPage(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "alertRuleName",required = false) String alertRuleName,
            @RequestParam(value = "functionName",required = false) String functionName,
            @RequestParam(value = "productKey",required = false) String productKey){
        PageResponse<AlertRuleVo> page=alertRuleService.getAlertRulePage(pageNum,pageSize,alertRuleName,productKey,functionName);
        return ResponseResult.success(page);
    }

     @ApiOperation(value = "新增报警规则")
    @PostMapping(path = "/create")
    public ResponseResult<Void> createRule(@RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.createRule(alertRuleDto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "获取单个告警规则")
    @GetMapping(path = "/read/{id}")
    public ResponseResult<AlertRuleVo> readAlertRule(@PathVariable Long id){
        AlertRuleVo alertRuleVo=alertRuleService.readAlertRule(id);
        return ResponseResult.success(alertRuleVo);
    }

    @ApiOperation(value = "更新告警规则")
    @PutMapping(path = "/update/{id}")
    public ResponseResult<Void> updateAlertRule(@PathVariable Long id,@RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.updateAlertRule(id,alertRuleDto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "删除告警规则")
    @PutMapping(path = "/delete/{id}")
    public ResponseResult<Void> deleteAlertRule(@PathVariable Long id){
        alertRuleService.deleteAlertRule(id);
        return ResponseResult.success();
    }

    @ApiOperation(value = "启用/禁用")
    @PutMapping(path = "/{id}/status/{status}")
    public ResponseResult<Void> enableOrDisable(@PathVariable Long id,@PathVariable Integer status){
        alertRuleService.enableOrDisable(id,status);
        return ResponseResult.success();
    }
}
